﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
using DigestChecker.Properties;
using System.Diagnostics.CodeAnalysis;

namespace DigestChecker
{
    internal partial class Form1 : Form
    {
        private Digest _digest;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadAlgorithms();
        }

        private void LoadAlgorithms()
        {
            var algComboBox = AlgorithmCombo;

            var algorithms = Enum.GetValues(typeof (AlgorithmType));

            var items = new List<Algorithm>();
            foreach (var algorithm in algorithms)
            {
                items.Add(new Algorithm
                              {
                                  AlgorithmName = Enum.GetName(typeof(AlgorithmType), algorithm).ToUpperInvariant(),
                                  AlgorithmType = (int)algorithm,
                              });
                
            }
            algComboBox.DataSource = items;
            algComboBox.ValueMember = "AlgorithmType";
            algComboBox.DisplayMember = "AlgorithmName";
        }

        

        private void ResetForm()
        {
            compareResultLabel.Visible = false;
            toolStripStatusLabel1.Text = "";
            hashStringTextBox.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            ResetForm();
        }

        private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
        {
            var fileDialog = (OpenFileDialog)sender;
            
            FileNameTextBox.Text = fileDialog.FileName;

            ResetForm();

        }

        private bool ValidateOptions()
        {
            toolStripStatusLabel1.Text = string.Format(CultureInfo.CurrentCulture, Resources.StatusLabel_Validating);
            var algName = AlgorithmCombo.Text;

            if (string.IsNullOrEmpty(algName))
            {
                MessageBox.Show(
                    Resources.Message_SelectValidAlgorithm,
                    Resources.Message_TitleError,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
                   
                toolStripStatusLabel1.Text = string.Format(CultureInfo.CurrentCulture, Resources.StatusLabel_ValidateError);
                return false;
            }

            var fileName = FileNameTextBox.Text;
            if (string.IsNullOrEmpty(fileName))
            {
                MessageBox.Show(
                    Resources.Message_InvalidFileName,
                    Resources.Message_TitleError,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error,
                    MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
                   
                toolStripStatusLabel1.Text = string.Format(CultureInfo.CurrentCulture, Resources.StatusLabel_ValidateError);
                return false;
            }
            return true;
        }

        private void CalculateButton_Click(object sender, EventArgs e)
        {
            if (!ValidateOptions())
                return;
           
            toolStripStatusLabel1.Text = string.Format(CultureInfo.CurrentCulture, Resources.StatusLabel_Calculating);
            timer1.Start();

            var algorithm = AlgorithmCombo.SelectedValue;
            var algorithmType = (AlgorithmType)Enum.Parse(typeof (AlgorithmType), algorithm.ToString());

            _digest = new Digest(FileNameTextBox.Text, algorithmType);
            _digest.ComputeHash();

            hashStringTextBox.Text = _digest.Hash;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (_digest.IsComplete)
            {
                timer1.Stop();
                toolStripStatusLabel1.Text = string.Format(CultureInfo.CurrentCulture, Resources.StatusLabel_CompletedCalculation);
            }
            hashStringTextBox.Text = _digest.Hash;
        }

        private void CompareButton_Click(object sender, EventArgs e)
        {
            var resultHash = hashStringTextBox.Text.Trim();
            var inputHash = inputHashTextBox.Text.Trim();

            // Create a StringComparer an compare the hashes.
            StringComparer comparer = StringComparer.OrdinalIgnoreCase;

            if (0 == comparer.Compare(resultHash, inputHash))
            //if (resultHash.ToUpperInvariant().Equals(inputHash.ToUpperInvariant()))
            {
                compareResultLabel.ForeColor = Color.DarkBlue;
                compareResultLabel.Text = String.Format(CultureInfo.CurrentCulture, Resources.CompareResult_Md5ChecksumAreEqual);
                compareResultLabel.Visible = true;
            }
            else
            {
                compareResultLabel.ForeColor = Color.Red;
                compareResultLabel.Text = String.Format(CultureInfo.CurrentCulture, Resources.CompareResult_Md5ChecksumAreDifferent);
                compareResultLabel.Visible = true;
            }
        }


        private class Algorithm
        {
            public int AlgorithmType { get; set; }

            public string AlgorithmName { get; set; }
        }
    }
}
